namespace AltNetUK.Models.Inputs
{
    using System;
    using System.Globalization;
    using System.Text;

    public sealed class TextArea : FormControl
    {
        private IValidateText _validator;

        public TextArea(IValidateText validator)
        {
            this.Validator = validator;
        }

        public IValidateText Validator
        {
            get
            {
                return this._validator;
            }

            set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }

                this._validator = value;
            }
        }

        public string Render(int tabIndex, string value, int cols, int rows)
        {
            if (1 > tabIndex)
            {
                throw new ArgumentOutOfRangeException("tabIndex");
            }
            else if (1 > cols)
            {
                throw new ArgumentOutOfRangeException("cols");
            }
            else if (1 > rows)
            {
                throw new ArgumentOutOfRangeException("cols");
            }

            StringBuilder buffer = new StringBuilder();

            buffer.Append(new Label(this, value));
            buffer.AppendFormat(
                CultureInfo.InvariantCulture,
                "<textarea id='{0}' name='{1}' tabindex='{2}' title='{3}' cols='{4}' rows='{5}'>{6}</textarea>",
                this.Id,
                this.Name,
                tabIndex,
                this.Title,
                cols,
                rows,
                value
                );

            return buffer.ToString();
        }

        public override ValidationResult Validate(string value)
        {
            return this.Validator.Validate(value);
        }
    }
}